package bbs2.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import bbs2.model.User;

public class UserDAO {
    
    private static User makeUser(ResultSet rs) throws Exception {
        User user = new User();
        user.setId(rs.getInt("ID"));
        user.setLoginId(rs.getString("loginID"));
        user.setPasswd(rs.getString("passwd"));
        user.setName(rs.getString("name"));
        user.setEmail(rs.getString("email"));
        user.setEnabled(rs.getBoolean("enabled"));
        return user;
    }
    
    public static User selectByLoginID(String loginID) throws Exception {
        User user = null;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;        
        try {
            connection = DB.getConnection();
            String sql = "SELECT * FROM [User] WHERE [loginID] = ?"; 
            statement = connection.prepareStatement(sql);
            statement.setString(1, loginID);
            resultSet = statement.executeQuery();
            if (resultSet.next()) user = makeUser(resultSet);
        } finally {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
        return user;
    }
    
    public static User selectByID(int id) throws Exception {
        User user = null;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;        
        try {
            connection = DB.getConnection();
            String sql = "SELECT * FROM [User] WHERE [ID] = ?"; 
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            if (resultSet.next()) user = makeUser(resultSet);
        } finally {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
        return user;
    }
    
    public static ArrayList<User> selectByUserGroupID(int userGroupID) throws Exception {
        ArrayList<User> a = new ArrayList<User>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;        
        try {
            connection = DB.getConnection();
            String sql = "EXEC userSelectByUserGroupID ?"; 
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userGroupID);
            resultSet = statement.executeQuery();
            while (resultSet.next()) 
                a.add(makeUser(resultSet));
        } finally {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
        return a;
    }
    
    public static ArrayList<User> selectPage(int currentPage, int pageSize, int order, int srchType, String srchText, int category) throws Exception {
        ArrayList<User> list = new ArrayList<User>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;        
        try {
            connection = DB.getConnection();
            String sql = "EXEC userSelectPage ?, ?, ?, ?, ?, ?"; 
            statement = connection.prepareStatement(sql);
            statement.setInt(1, currentPage);
            statement.setInt(2, pageSize);
            statement.setInt(3, order);
            statement.setInt(4, srchType);
            statement.setString(5, srchText);
            statement.setInt(6, category);
            resultSet = statement.executeQuery();
            while (resultSet.next()) 
                list.add(makeUser(resultSet));
        } finally {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
        return list;
    }
    
    public static int getRecordCount(int srchType, String srchText, int category) throws Exception {
        int count = 0;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;        
        try {
            connection = DB.getConnection();
            String sql = "EXEC userRecordCount ?, ?, ?"; 
            statement = connection.prepareStatement(sql);
            statement.setInt(1, srchType);
            statement.setString(2, srchText);
            statement.setInt(3, category);
            resultSet = statement.executeQuery();
            if (resultSet.next()) 
                count = resultSet.getInt(1);
        } finally {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
        return count;
    }
    
    public static void insert(String loginID, String passwd, String name, String email) throws Exception {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DB.getConnection();
            String sql = "INSERT [User] ([loginID], [passwd], [name], [email], [enabled]) VALUES (?, ?, ?, ?, 1)"; 
            statement = connection.prepareStatement(sql);
            statement.setString(1, loginID);
            statement.setString(2, passwd);
            statement.setString(3, name);
            statement.setString(4, email);
            statement.executeUpdate();
        } finally {
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
    }
    
    public static void updateInfo(int id, String loginID, String name, String email, boolean enabled) throws Exception {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DB.getConnection();
            String sql = "UPDATE [User] SET [loginID] = ?, [name] = ?, [email] = ?, [enabled] = ? WHERE [ID] = ?"; 
            statement = connection.prepareStatement(sql);
            statement.setString(1, loginID);
            statement.setString(2, name);
            statement.setString(3, email);
            statement.setBoolean(4, enabled);
            statement.setInt(5, id);
            statement.executeUpdate();
        } finally {
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
    }    
    
    public static void updatePasswd(int id, String passwd) throws Exception {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DB.getConnection();
            String sql = "UPDATE [User] SET [passwd] = ? WHERE [id] = ?"; 
            statement = connection.prepareStatement(sql);
            statement.setString(1, passwd);
            statement.setInt(2, id);
            statement.executeUpdate();
        } finally {
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
    }

}
